package com.bwie.goods.service.impl;

import com.bwie.common.domain.Store;
import com.bwie.common.domain.request.StoreRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.goods.mapper.StoreMapper;
import com.bwie.goods.service.StoreService;
import com.bwie.goods.util.FastUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class StoreServiceimpl implements StoreService {
    @Autowired
    StoreMapper storeMapper;

    @Autowired
    FastUtil fastUtil;

    @Override
    public Result<PageResult<Store>> list(StoreRequest storeRequest) {
        PageHelper.startPage(storeRequest.getPageNum(),storeRequest.getPageSize());

        List<Store> list=storeMapper.list(storeRequest,storeRequest.getStoreAddress(),storeRequest.getStoreName());

        PageInfo<Store> storePageInfo = new PageInfo<>(list);

        return PageResult.toResult(storePageInfo.getTotal(),list);
    }

    @Override
    public Result upload(MultipartFile file) {

        String upload=null;
        try {
            upload = fastUtil.upload(file);
            if(upload==null){
                return Result.error("图片上传失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success("http://124.223.202.119:8888/"+upload,"上传成功");
    }

    @Override
    public Result add(Store store) {
        //获取数据
        Integer id=storeMapper.findByName(store.getStoreName());
        //验证门店的名称唯一性
        if(id!=null){
           return Result.error("已存在该商店,请重新添加");
        }

        Integer i=storeMapper.add(store);

        return Result.success(i>0?200:500,i>0?"添加成功":"添加失败");
    }


}
